package com.scott.crm.delay.controller;

import com.scott.base.BaseBean;
import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.base.utils.AESUtil;
import com.scott.crm.delay.bean.ClazzUserBean;
import com.scott.crm.delay.bean.DelayBean;
import com.scott.crm.delay.service.DelayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 方雅浩
 * @date 2021-11-29 17:03:23
 * @Version 1.0
 */
@CrossOrigin
@RestController
public class DelayController {

    @Autowired
    private DelayService service;



    @RequestMapping(value = "/delay/showStudent",method = RequestMethod.POST)
    public DataRes showStudent(@RequestBody BaseBean baseBean) throws Exception{

        String token=baseBean.getToken();


        String userId = AESUtil.decrypt(token, AESUtil.KEY);

        List<ClazzUserBean> allStudents = service.findAllStudentByClazzId(Integer.parseInt(userId));


        if(!CollectionUtils.isEmpty(allStudents)){
            return DataUtil.success(allStudents);
        }else{
            return DataUtil.errors();
        }
    }


    @RequestMapping(value = "/delay/add",method = RequestMethod.POST)
    public DataRes add(@RequestBody DelayBean delayBean) throws Exception{

        if(delayBean == null){
            return null;
        }


        int counter = service.addDelayRecord(delayBean);
        if(counter ==1 ){
            return DataUtil.success(delayBean);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "/delay/update",method = RequestMethod.POST)
    public DataRes update(@RequestBody DelayBean delayBean) throws Exception{

        if(delayBean == null){
            return null;
        }


        int counter = service.updateDelayRecord(delayBean);
        if(counter ==1 ){
            return DataUtil.success(delayBean);
        }else{
            return DataUtil.errors();
        }
    }
    @RequestMapping(value = "/delay/delete",method = RequestMethod.POST)
    public DataRes delete(@RequestBody Map<String,List<Integer>> map) throws Exception{

        if(CollectionUtils.isEmpty(map)||map.get("ids").isEmpty()){
            return DataUtil.errors();
        }

        List<Integer> ids = map.get("ids");


        int counter = service.deleteDelayRecord(ids);
        if(counter >0){
            return DataUtil.success(counter);
        }else{
            return DataUtil.errors();
        }
    }


    @RequestMapping(value = "/delay/list",method = RequestMethod.POST)
    @ResponseBody
    public DataRes find(@RequestBody BaseBean baseBean){

        String token=baseBean.getToken();
        Integer pageNumber=baseBean.getPageNumber();
        Integer pageSize=baseBean.getPageSize();


        if(pageNumber<1){
            pageNumber=1;
        }

        String userId = null;
        try {
            userId = AESUtil.decrypt(token, AESUtil.KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        List<DelayBean> delayBeanList = service.findDelayRecord(Integer.parseInt(userId),begin,end);

        Map<String,Object> rs= new HashMap<>();
        rs.put("delayList",delayBeanList);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<DelayBean> allDelays = service.findAllDelayRecord(Integer.parseInt(userId));
        if(!CollectionUtils.isEmpty(allDelays)){
            Integer totalCounter = allDelays.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }



        return DataUtil.success(rs);

    }

    @RequestMapping(value = "/delay/group",method = RequestMethod.GET)
    @ResponseBody
    public DataRes findAll(@RequestParam(value="token") String token){





        String userId = null;
        try {
            userId = AESUtil.decrypt(token, AESUtil.KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String,Object> rs= new HashMap<>();

        List<DelayBean> group = service.findAllGroup(Integer.parseInt(userId));
        rs.put("group",group);

        return DataUtil.success(rs);

    }

}
